package com.jt.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jt.pojo.Dept;
import com.jt.pojo.DeptUser;
import com.jt.pojo.Result;
import com.jt.pojo.User;
import com.jt.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

@RestController
@RequestMapping("system/dept")
public class DeptController {
    @Autowired
    private DeptService deptService;

    // 查询部门列表树形
    @GetMapping("/deptList")
    public Result getDeptList() {
        List<Dept> list = deptService.getDeptList();
        return Result.success(list);
    }

    // 部门下拉框
    @GetMapping("/deptAll")
    public Result deptAll() {
        List<Dept> list = deptService.list();
        return Result.success(list);
    }

    // 通过部门id查询用户列表
    @GetMapping("/getUserListByDeptId")
    public Result getUserListByDeptId() {
        /*这里部门ID没用，我直接在前端去赛选的*/
        List<User> list = deptService.getUserListByDeptId();
        return Result.success(list);
    }

    //新增部门
    @PostMapping("/insert")
    public Result insert(@RequestBody Dept dept) {
        Dept one = deptService.getOne(new QueryWrapper<Dept>().eq("code", dept.getCode()));
        if (!Objects.isNull(one)) {
            return Result.error("部门编码已存在");
        }
        if (Objects.isNull(dept.getParentId())) {
            dept.setParentId("0");
            boolean save = deptService.save(dept);
            if (save) {
                return Result.success("添加成功");
            }
            return Result.error("添加失败");
        } else {
            boolean b = deptService.save(dept);
            if (b) {
                return Result.success("添加成功");
            }
            return Result.error("添加成功");
        }

    }

    //修改部门
    @PostMapping("/update")
    public Result update(@RequestBody Dept dept) {
        boolean b = deptService.updateById(dept);
        if (b) {
            return Result.success("修改成功");
        }
        return Result.error("修改失败");
    }

    //删除部门
    @GetMapping("/delete")
    public Result delete(String id) {
        List<String> ids = deptService.list(new QueryWrapper<Dept>().eq("parentId", id)).stream().map(Dept::getId).collect(Collectors.toList());
        ids.add(id);
        boolean b = deptService.removeByIds(ids);
        if (b) {
            return Result.success("删除成功");
        }
        return Result.error("删除失败");
    }



    //部门新增用户
    @PostMapping("/insertUserList")
    public Result insertUserList(@RequestBody Map<String, Object> map) {
        return deptService.insertUserList(map);
    }

    //部门移除用户
    @PostMapping("/removeUser")
    public Result removeUser(@RequestBody DeptUser deptUser) {
        boolean b=deptService.removeUser(deptUser);
        if (b) {
            return Result.success("移除成功");
        }
        return Result.error("修改失败");
    }

}
